package com.zhao.Util;
 
 
import java.util.*;

/**
 *  <p>
 *      年龄工具类
 *  </p>
 *
 * @author 简书 -- copy
 * @date 2019-09-06
 * */
public class AgeUtil {
    public static String getAgeDetail(String date){
        //如果有空格
        int index = date.indexOf(" ");
        if (index != -1){
            date = date.substring(0 , index);
        }
 
        String[] data = date.split("-");
 
        Calendar birthday =new GregorianCalendar(Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]));
 
        Calendar now = Calendar.getInstance();
 
        int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);
        //月份从0开始计算，所以需要+1
        int month = now.get(Calendar.MONTH) +1 - birthday.get(Calendar.MONTH);
 
        int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
 
        //按照减法原理，先day相减，不够向month借；然后month相减，不够向year借；最后year相减。
 
        if (day <0) {
 
            month -=1;
 
            now.add(Calendar.MONTH, -1);//得到上一个月，用来得到上个月的天数。
 
            day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);
 
        }
 
        if (month <0) {
 
            month = (month +12) %12;
 
            year--;
 
        }
 
        //System.out.println("年龄：" + year +"岁" + month +"月" + day +"天");
 
        StringBuffer tag =new StringBuffer();
 
        if (year >0) {
 
            tag.append(year +"岁");
 
        }
 
        if (month >0) {
 
            tag.append(month +"个月");
 
        }
 
        if (day >0) {
 
            tag.append(day +"天");
 
        }
 
        if (year ==0 && month ==0 && day ==0) {
 
            tag.append("今日出生");
 
        }
 
        return String.valueOf(tag);
    }
    public static String getAge(String date){
        String ageDetail = getAgeDetail(date);
        return ageDetail.substring(0, ageDetail.indexOf("岁"));
    }

    /**
     *
     * @param ages 处理过的用户年龄集合
     * @return 图表 需要的JSON格式数据
     */
    public static List<Map<String,Object>> getAnalyzeAge(List<String> ages){
        Map<String,Object> map=new HashMap<>();//'0-10'
        map.put("name","未知");
        map.put("value",0);
        Map<String,Object> map1=new HashMap<>();//'0-10'
        map1.put("name","0-10");
        map1.put("value",0);
        Map<String,Object> map2=new HashMap<>();//'11-20'
        map2.put("name","11-20");
        map2.put("value",0);
        Map<String,Object> map3=new HashMap<>();//'21-30
        map3.put("name","21-30");
        map3.put("value",0);
        Map<String,Object> map4=new HashMap<>();//'31-40'
        map4.put("name","31-40");
        map4.put("value",0);
        Map<String,Object> map5=new HashMap<>();//'41-50
        map5.put("name","41-50");
        map5.put("value",0);
        Map<String,Object> map6=new HashMap<>();//'50以上'
        map6.put("name","50以上");
        map6.put("value",0);
        for (String age1:ages) {
            Integer age =null;
            if (age1!=null){
                age=Integer.parseInt(getAge(age1));
            }

            if (age==null){
                map.put("value",((Integer)map.get("value")+1));
            } else if (age<11){
                map1.put("value",((Integer)map1.get("value")+1));
            }else if (age<21){
                map2.put("value",((Integer)map2.get("value")+1));
            }else if (age<31){
                map3.put("value",((Integer)map3.get("value")+1));
            }else if (age<41){
                map4.put("value",((Integer)map4.get("value")+1));
            }else if (age<51){
                map5.put("value",((Integer)map5.get("value")+1));
            }else {
                map6.put("value",((Integer)map6.get("value")+1));
            }
        }
        List<Map<String,Object>> list=new ArrayList<>();
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        list.add(map6);
        for (int i = 0; i <list.size() ; i++) {
            Map<String, Object> stringObjectMap = list.get(i);
            if ((Integer) stringObjectMap.get("value")==0) {
                list.remove(i);
                i--;
            }
        }
        return list;
    }
}